import axios from 'axios'
import { Message } from 'element-ui'
const request = axios.create({
  baseURL: 'http://127.0.0.1:3000',
  timeout: 3000
  // withCredentials: true , //允许跨域
})

request.interceptors.request.use(
  config => {
    //判断token是否存在
    if (localStorage.getItem('token')) {
      // 在请求头中添加token
      config.headers.Authorization = localStorage.getItem('token')
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)
request.interceptors.response.use(
  function (response) {
    if (response.data.status == 401) {
      Message({
        type: 'error',
        message: '账号信息已过期，请重新登录',
        showClose: true,
        duration: 1000
      })
      setTimeout(() => {
        location.href = '/'
        localStorage.removeItem('token')
        localStorage.removeItem('ismanager')
      }, 1000)
    }
    return response.data
  },
  function (error) {
    return Promise.reject(error)
  }
)
export default request
